# Contributor: He Yangxuan <yangxuan8282@gmail.com>
# Contributor: Timo Teras <timo.teras@iki.fi>
# Maintainer: Milan P. Stanić <mps@arvanta.net>
pkgname=u-boot
pkgver=2022.04
pkgrel=1
pkgdesc="u-boot bootloader common files"
url="https://www.denx.de/wiki/U-Boot/"
arch="all !s390x !ppc64le"
license="GPL-2.0-or-later OFL-1.1 BSD-2-Clause BSD-3-Clause eCos-2.0 IBM-pibs
	ISC LGPL-2.0-only LGPL-2.1-only X11"
options="!check" # no tests
makedepends="$depends_dev
	bc
	bison
	dtc
	flex
	gnutls-dev
	linux-headers
	openssl-dev
	py3-setuptools
	python3-dev
	swig
	util-linux-dev
	"
if [ "$CARCH" = "aarch64" ]; then
	makedepends="$makedepends arm-trusted-firmware"
fi
source="https://ftp.denx.de/pub/u-boot/u-boot-${pkgver//_/-}.tar.bz2
	README.txt
	update-u-boot
	hifive-unmatched-ramdisk.patch
	fix-tools-build.patch
	fix-build-with-binutils-2.38.patch
	"
builddir="$srcdir"/u-boot-${pkgver//_/-}

if [ "$CARCH" = "riscv64" ]; then
	makedepends="$makedepends opensbi"
	export OPENSBI=/usr/share/opensbi/generic/firmware/fw_dynamic.bin
fi

# secfixes:
#   2021.04-r0:
#     - CVE-2021-27097
#     - CVE-2021-27138

case "$CARCH" in
arm*) board_configs="
	beagleboard:am335x_boneblack_vboot
	linksprite:Linksprite_pcDuino3_Nano
	cuboxi:mx6cuboxi
	raspberrypi:rpi_0_w,rpi,rpi_2,rpi_3_32b
	wandboard:wandboard
	qemu:qemu_arm
	odroid:odroid-xu3
	sunxi:LicheePi_Zero,Bananapi,Lamobo_R1,Cubieboard,Cubieboard2
	";;
aarch64) board_configs="
	thunderx:thunderx_88xx
	raspberrypi:rpi_3
	odroid:odroid-c2,odroid-n2
	libretech:libretech-cc
	rockchip:rockpro64-rk3399
	qemu:qemu_arm64
	sunxi:pine64-lts,pinebook,orangepi_3,teres_i,a64-olinuxino,a64-olinuxino-emmc,nanopi_neo2
	";;
riscv64) board_configs="
	qemu:qemu-riscv64
	unleashed:sifive_unleashed
	unmatched:sifive_unmatched
";;
esac

for board_config in $board_configs; do
	_allboards="$_allboards $pkgname-${board_config%%:*}"
done

subpackages="$_allboards $pkgname-tools"
case "$CARCH" in
	arm*|aarch64|riscv64) subpackages="$pkgname-all:_all $_allboards $pkgname-tools";;
esac

build() {
	cd "$builddir"
	touch include/config.h
	LC_ALL=C date +'#define U_BOOT_DATE "%b %d %C%y"' > include/timestamp_autogenerated.h
	LC_ALL=C date +'#define U_BOOT_TIME "%T"' >> include/timestamp_autogenerated.h

	local board_config board
	for board_config in $board_configs; do
		local configs="${board_config#*:}"
		for board in ${configs//,/ }; do
			msg "Building u-boot for $board"

			case "$board" in
				"pine64-lts"|"pinebook"|"teres_i"|"a64-olinuxino"|"a64-olinuxino-emmc")
					export BL31="/usr/share/arm-trusted-firmware/sun50i_a64/bl31.bin";;
				"orangepi_3")
					export BL31="/usr/share/arm-trusted-firmware/sun50i_h6/bl31.bin";;
			esac

			export BUILD_DIR="$builddir"/build/$board
			mkdir -p "$BUILD_DIR"
			make O="$BUILD_DIR" ${board}_config
			make O="$BUILD_DIR" all
		done
	done
	msg "Building u-boot-tools"
	make tools-only_defconfig
	make tools-all
}

package() {
	case "$CARCH" in
		mips64|x86*) return;;
	esac
	cd "$builddir"/build
	mkdir -p "$pkgdir"/usr/share/$pkgname "$pkgdir"/usr/sbin
	install "$srcdir"/README.txt "$pkgdir"/usr/share/$pkgname/README.txt
	install "$srcdir"/update-u-boot "$pkgdir"/usr/sbin
}

tools() {
	pkgdesc="u-boot bootloader utility tools"
	provides="uboot-tools"
	replaces="uboot-tools"
	mkdir -p $pkgdir
	mkdir $subpkgdir
	cd "$builddir"
	for tool in bmp_logo dumpimage env/fw_printenv \
		fit_check_sign fit_info gdb/gdbcont gdb/gdbsend gen_eth_addr img2srec \
		mkenvimage mkimage ncb proftool ubsha1 xway-swap-bytes; do
		install -D tools/$tool \
		$subpkgdir/usr/bin/$(basename $tool)
		done
	install -Dm644 tools/env/fw_env.config \
		"$subpkgdir/usr/share/doc/$pkgname/examples/fw_env.config"
	cd "$subpkgdir"/usr/bin
	ln -sf fw_printenv fw_setenv
}

_all() {
	pkgdesc="u-boot for all boards (meta package)"
	depends="$_allboards"

	mkdir -p "$subpkgdir"/
}

_split_boards() {
	cd "$builddir"/build
	pkgdesc="u-boot for $1"
	depends="u-boot"
	shift
	local board
	for board; do
		msg "Including board $board"
		mkdir -p "$subpkgdir"/usr/share/$pkgname/$board
		export BUILD_DIR="$builddir"/build/$board

		local board_images=""
		case "$board" in
			"sifive_unleashed") board_images="u-boot.itb spl/u-boot-spl.bin --" ;;
			"sifive_unmatched") board_images="u-boot.itb spl/u-boot-spl.bin --" ;;
		esac

		local ok=no
		for image in $board_images u-boot-sunxi-with-spl.bin -- MLO SPL u-boot.img -- u-boot.bin; do
			if [ "$image" = "--" ]; then
				[ "$ok" = yes ] && break
				continue
			fi
			if [ -e "$BUILD_DIR"/$image ]; then
				cp "$BUILD_DIR"/$image "$subpkgdir"/usr/share/$pkgname/$board
				ok=yes
			fi
		done
		[ "$ok" = yes ] || return 
	done
}

for board_config in $board_configs; do
	_board="${board_config%%:*}"
	_configs="${board_config#*:}"
	eval "$_board() { _split_boards $_board ${_configs//,/ }; }"
done

sha512sums="
113056e542db049eea3b1d1ccbbe52c79539a2f9bb6129d284ec9200dcffec7bd5969821913a9899631575b80991c84ad01bc845b4bba614e42e54466d9f0d64  u-boot-2022.04.tar.bz2
f8c9bb6e84d6f0620c976ac7ad5dd7ec7ff9dfdd4b1d03d2bf6653e7beccf80bdf2debfc92fb1f696dba92fb40287d3c45897e0078951451d0835cb61a5f16d1  README.txt
b5b5960a70c0edab38c208e5a41f2130c673334e05ef77dd4c839d59365d4243590a79bf3865914397b600550f4d0f69f8145e51a7ef05aa0190420fcc7ff02d  update-u-boot
1e8c95a37e7efb74f6519f66da44a0050417066aa2bd6d38e8a7e7135f501a9578dcc3701f66a170bf4cbd5c17b87d69ff80760979b1aa31d26e75be7cf7b97f  hifive-unmatched-ramdisk.patch
3b900d833ee7d82ee3bca6e8f8cb8e297359bb65c3f095a0d22377779eb8a233c5e566e5db6ce79f3076fbe6413d002941eb9cc450e3462337f46c1805472ce0  fix-tools-build.patch
aff151d67a8d6b280ae1db6ad00d920c98ae6a39088f9c0e369e23fffb877dc843c2d6664257845aa365fce8babd19bbafaf91690ee3204201a8aadf9c5c942e  fix-build-with-binutils-2.38.patch
"
